﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ClickToCall.ClickToCallToolbar
{
    static class Log
    {
        static string fileformat = @"{0}\ClickToCall.log";
        static string logformat = "{0}\t{1} -- {2}";
        static List<string> loglist = new List<string>();
        
        public static void AddToLog(string action, string message)
        {
            //Turn on to debug
            //loglist.Add(string.Format(logformat, DateTime.Now, action, message));
        }

        public static int Count
        {
            get { return loglist.Count; }
        }

        public static void Delete()
        {
            System.Configuration.Configuration conf =
                System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal);
            string file = string.Format(fileformat, System.IO.Path.GetDirectoryName(conf.FilePath));

            try
            {
                if (System.IO.File.Exists(file))
                    System.IO.File.Delete(file);
            }
            catch { }
        }

        public static void Save()
        {

            System.Configuration.Configuration conf =
                System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal);
            string file = string.Format(fileformat, System.IO.Path.GetDirectoryName(conf.FilePath));

            System.IO.FileStream fs = null;
            System.IO.StreamWriter sw = null;
            try
            {
                if (System.IO.File.Exists(file))
                    System.IO.File.Delete(file);
                fs = new System.IO.FileStream(file, System.IO.FileMode.Create);
                sw = new System.IO.StreamWriter(fs);
                foreach (string s in loglist)
                {
                    sw.WriteLine(s);
                }
            }
            catch
            {
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                    sw = null;
                }
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
            }
        }
    }
}
